package tt.dz.egobus.servicemanager.business.convert;

import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.servicemanager.bean.CornConverter;
import tt.dz.egobus.servicemanager.util.DateUtils;

import java.util.Date;

/**
 * @author Anson
 * @date 17-8-29
 * @name 预约时间在redis存储为yyyy-MM-dd转换器
 * @since 1.0.7
 */
public class BespeakTimeRedisRestoreConvertor implements CornConverter<Date, String> {

    private static final String PATTERN = DateUtils.DATE_TO_STRING_SHORT_PATTERN;

    @Override
    public Converter<Date, String> convertCornA() {
        return new Converter<Date, String>() {
            @Override
            public String convert(Date source) {
                return BespeakTimeRedisRestoreConvertor.this.convertDateToStr(source);
            }
        };
    }

    @Override
    public Converter<String, Date> convertCornB() {
        return new Converter<String, Date>() {
            @Override
            public Date convert(String source) {
                return BespeakTimeRedisRestoreConvertor.this.convertStrToDate(source);
            }
        };
    }

    /**
     * @param dateStr yyyy-MM-dd型的字符串日期
     * @author Anson
     * @date 17-8-29
     * @name yyyy-MM-dd型的字符串转换转换成日期
     * @since 1.0.7
     */
    private Date convertStrToDate(String dateStr) {
        return DateUtils.stringToDate(dateStr, PATTERN);
    }

    /**
     * @param dateStr yyyy-MM-dd型的字符串日期
     * @author Anson
     * @date 17-8-29
     * @name yyyy-MM-dd型的字符串转换转换成日期
     * @since 1.0.7
     */
    private String convertDateToStr(Date date) {
        return DateUtils.DateToString(date, PATTERN);
    }
}
